package com.ktg.mes.chanjet.controller;


import com.chanjet.openapi.sdk.java.exception.ChanjetApiException;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.core.domain.Result;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.chanjet.entity.TpItem;
import com.ktg.mes.chanjet.entity.TpItemType;
import com.ktg.mes.chanjet.response.InventoryClassResponse;
import com.ktg.mes.chanjet.service.TpItemService;
import com.ktg.mes.chanjet.service.TpItemTypeService;
import com.ktg.mes.chanjet.spi.ChanjetSpi;
import com.ktg.mes.md.domain.MdItem;
import com.ktg.mes.md.service.IMdItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;


@RestController
@Slf4j
@RequestMapping("/tplus/materiel")
public class TplusMaterielController extends BaseController {


    @Autowired
    ChanjetSpi chanjetSpi;

    @Autowired
    TpItemService tpItemService;


    @Autowired
    TpItemTypeService tpItemTypeService;

    @Autowired
    IMdItemService mdItemService;


    @GetMapping("/list")
    public Result<Object> list(TpItem vo) {
        startPage();
        List<TpItem> tpItems = tpItemService.selectTpItemList(vo);
        return Result.OK(getDataTable(tpItems));
    }

    @GetMapping(value = "/{itemId}")
    public Result<Object> getInfo(@PathVariable Long itemId){
        return Result.OK(tpItemService.getById(itemId));
    }

    @PostMapping
    public Result<Object> add(@Validated @RequestBody TpItem tpItem){
        if(UserConstants.NOT_UNIQUE.equals(tpItemService.checkItemCodeUnique(tpItem))){
            return Result.ERROR("新增物料"+tpItem.getItemCode()+"失败，物料编码已存在");
        }
        if(UserConstants.NOT_UNIQUE.equals(tpItemService.checkItemNameUnique(tpItem))){
            return Result.ERROR("新增物料"+tpItem.getItemCode()+"失败，物料名称已存在");
        }

        TpItemType type =tpItemTypeService.getById(tpItem.getItemTypeId());
        if(StringUtils.isNotNull(type)){
            tpItem.setItemTypeCode(type.getItemTypeCode());
            tpItem.setItemTypeName(type.getItemTypeName());
            tpItem.setItemOrProduct(type.getItemOrProduct());
        }

        tpItem.setCreateBy(getUsername());
        tpItem.setCreateTime(new Date());
        tpItemService.save(tpItem);
        return Result.OK(tpItem);
    }

    @PutMapping
    public Result<Object> edit(@Validated @RequestBody TpItem tpItem){
        if(UserConstants.NOT_UNIQUE.equals(tpItemService.checkItemCodeUnique(tpItem))){
            return Result.ERROR("新增物料"+tpItem.getItemCode()+"失败，物料编码已存在");
        }
        if(UserConstants.NOT_UNIQUE.equals(tpItemService.checkItemNameUnique(tpItem))){
            return Result.ERROR("新增物料"+tpItem.getItemCode()+"失败，物料名称已存在");
        }
        TpItemType type =tpItemTypeService.getById(tpItem.getItemTypeId());
        if(StringUtils.isNotNull(type)){
            tpItem.setItemTypeCode(type.getItemTypeCode());
            tpItem.setItemTypeName(type.getItemTypeName());
            tpItem.setItemOrProduct(type.getItemOrProduct());
        }


        if(StringUtils.isNotNull(tpItem.getSafeStockFlag())&& "N".equals(tpItem.getSafeStockFlag())){
            tpItem.setMinStock(0D);
            tpItem.setMaxStock(0D);
        }

        tpItem.setUpdateBy(getUsername());
        tpItem.setUpdateTime(new Date());
        tpItemService.updateById(tpItem);
        return Result.OK();
    }


    @GetMapping("/units")
    public Result<Object> getUnitList(@RequestParam(name="code") String code) throws ChanjetApiException {
        InventoryClassResponse unitList = chanjetSpi.getUnitList(code);
        return Result.OK(unitList.getData());
    }

    @DeleteMapping("/{itemIds}")
    public Result<Object> remove(@PathVariable List<Long> itemIds){
        System.out.println(itemIds);
        tpItemService.removeBatchByIds(itemIds);
        return Result.OK("删除成功");
    }


    @GetMapping("/syncEmsTypeData")
    public Result<Object> sysncEmsTypeData(){
        List<MdItem> mdItems = mdItemService.selectMdItemAll();
        List<MdItem> mdBackUpItems = mdItemService.selectMdItemBackUpAll();
        if(mdItems.size() == 0 || mdBackUpItems.size() == 0) return Result.ERROR("无数据更新");
        for (MdItem item :mdItems) {
            for(MdItem item2 :mdBackUpItems){
                if(item.getItemCode().equals(item2.getItemCode())){
                    item.setProcessProductId(item2.getProcessProductId());
                    item.setProcessProductName(item2.getProcessProductName());
                }
            }
        }
        mdItemService.saveOrUpdateBatch(mdItems);
        return Result.OK();
    }


}
